import {computed, ref} from "vue";
import {ColumnsType} from "ant-design-vue/es/table";
import {PaginationConfig} from "ant-design-vue/es/pagination";
import {IListItem} from "@/views/Admin/Main/types";
import useAdminStore from "@/store/adminModule";
import {ADMIN_LIST_DEFAULT_PAGE_SIZE} from "@/constants/data";

export const isLoading = ref(false)
export const adminStore = useAdminStore()

export const dataSource: any = computed(() => {
    return adminStore.adminInfo.data.map((item, index) => {
        const listItem: IListItem = {
            key: index,
            id: item.id,
            username: item.username,
            password: item.password,
            permission: item.permission
        }
        return listItem
    })
})
export const columns: ColumnsType<any> = [
    {
        title: 'id',
        dataIndex: 'id',
        key: 'id',
    },
    {
        title: '用户名',
        dataIndex: 'username',
        key: 'username',
        align: "center"
    },
    {
        title: '密码',
        dataIndex: 'password',
        key: 'password',
        align: "center"
    },
    {
        title: '管理等级',
        dataIndex: 'permission',
        key: 'permission',
        align: "center"
    },
    {
        title: '操作',
        dataIndex: 'operation',
        key: 'operation',
        align: "center"
    },
]

// 表格分页配置
export const paginationObj = computed<PaginationConfig>(() => {
    return {
        current: adminStore.adminInfo.page_info.current_page,
        defaultPageSize: ADMIN_LIST_DEFAULT_PAGE_SIZE,
        pageSize: adminStore.adminInfo.page_info.page_size,
        total: adminStore.adminInfo.page_info.total_count,
        onChange: async (current_page, page_size) => {
            isLoading.value = true
            await adminStore.requestAdminListAction({
                current_page,
                page_size
            })
            isLoading.value = false
        }
    }
})